package CamEndzyx;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

// 继承JPanel 重写paint 方法
public class ImageUtils{

    public static int p = 50;
    ImagePanel imgPanel = new ImagePanel();

    public int[][] readImagePix(String path){
        // 文件对象
        File file = new File (path);
        BufferedImage readimg = null;
        try {
            readimg = ImageIO.read (file);
        } catch (IOException e) {
            e.printStackTrace ();
        }
        int width = readimg.getWidth ();
        int height = readimg.getHeight ();
        int height0 = 0;
        int width0 = 0;
        if(width >= height){
            height0 = width;
            width0 = width;
        }
        if(width < height){
            width0 = height;
            height0 = height;
        }
        imgPanel.width = width;
        imgPanel.height= height;
        int[][] imgArray = new int[width0][height0];
        for(int i = 0; i < width; i++){
            for(int j = 0; j < height; j++){
                imgArray[i][j] = readimg.getRGB (i, j);
            }
        }
        return imgArray;
    }

    public int getRgbGray(int numPixels){
        // byte -128 127
        int red = (numPixels>>16)&0xFF;
        int green = (numPixels>>8)&255;
        int blue = (numPixels>>0)&255;
        // 灰度 -- 减少计算量 以及 更方便计算
        int gray = (red + green + blue) / 3;
        return gray;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_00(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage ( imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        for(int i = 0; i < imgArray.length; i++){
            for(int j = 0; j < imgArray[i].length; j++){
                buffimg.setRGB (i,j,imgArray[i][j]);
            }
        }
        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_01(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage ( imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);

        System.out.print("iup = "+p);
        if(p < 20)p = 20;

        //可使用fillRect

        for(int i = p/10; i < imgArray.length; i += (2*(p/10)+1)){
            for(int j = p/10; j < imgArray[i].length; j += (2*(p/10)+1)){

                int mm,nn;
                if(i+(p/10+1)>imgArray.length) {
                    mm = imgArray.length;
                }else {
                    mm = i+(p/10+1);
                }
                if(j+(p/10+1)>imgArray[i].length) {
                    nn = imgArray[i].length;
                }else {
                    nn = j+(p/10+1);
                }

                for(int m=i-p/10;m<mm;m++) {
                    for(int n=j-p/10;n<nn;n++) {
                        buffimg.setRGB (m, n, imgArray[i][j]);
                    }
                }
            }
        }

        for(int i = imgArray.length-(p/10+1); i < imgArray.length; i++) {
            int a = imgArray[i].length/(p/10*2+1);
            for(int m = 0; m < a; m++) {
                for(int j = (p/10*2+1)*m; j < (p/10*2+1)*(m+1); j++){
                    buffimg.setRGB (i, j, imgArray[imgArray.length-1][(p/10*2+1)*m+(p/10+1)]);
                }
            }
        }

        int b = imgArray.length/(p/10*2+1);
        for(int m = 0; m < b; m++) {
            for(int i = (p/10*2+1)*m; i < (p/10*2+1)*(m+1); i++){
                for(int j = imgArray[i].length-(p/10+1); j < imgArray[i].length; j++) {
                    buffimg.setRGB (i, j, imgArray[(p/10*2+1)*m+(p/10+1)][imgArray[i].length-1]);
                }
            }
        }

        for(int i = imgArray.length-(p/10+1); i < imgArray.length; i++){
            for(int j = imgArray[i].length-(p/10+1); j < imgArray[i].length; j++) {
                buffimg.setRGB (i, j, imgArray[imgArray.length-1][imgArray[i].length-1]);
            }
        }

        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_02(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        for(int i = 0; i < imgArray.length; i++){
            for(int j = 0; j < imgArray[i].length; j++){
                int num1 = imgArray[i][j];
                int gray1 = getRgbGray (num1);
                Color color = new Color (gray1,gray1,gray1);
                buffimg.setRGB (i, j, color.getRGB ());
            }
        }
        return buffimg;
    }

    // 根据二维数组 绘制图片
    public BufferedImage drawImage_03(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        for(int i = 0; i < imgArray.length - 1; i++){
            for(int j = 1; j < imgArray[i].length - 1; j++){
                int num1 = imgArray[i][j];
                int num2 = imgArray[i + 1][j + 1];
                int num3 = imgArray[i + 1][j - 1];
                int num4 = imgArray[i + 1][j];
                int num5 = imgArray[i][j + 1];
                int gray1 = getRgbGray (num1);
                int gray2 = getRgbGray (num2);
                int gray3 = getRgbGray (num3);
                int gray4 = getRgbGray (num4);
                int gray5 = getRgbGray (num5);
                // 比较相邻的像素值 差距过大表示此处颜色差距大 应该是轮廓边框
                if((Math.abs (gray1 - gray2) > (7+p/10))||(Math.abs (gray1 - gray3) > (7+p/10))||(Math.abs (gray1 - gray4) > (7+p/10))||(Math.abs (gray1 - gray5) > (7+p/10))){
                    // 绘制为白色
                    buffimg.setRGB (i, j, Color.WHITE.getRGB ());
                } else{
                    // 绘制为黑色
                    buffimg.setRGB (i, j, Color.BLACK.getRGB ());
                }
            }
        }
        return buffimg;
    }

    // 根据二维数组 绘制图片
    public BufferedImage drawImage_04(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        for(int i = 0; i < imgArray.length; i++){
            for(int j = 0; j < imgArray[i].length; j++){
                int num1 = imgArray[i][j];
                int gray1 = getRgbGray (num1);
                if(gray1 > 78+p){
                    // 绘制为白色
                    buffimg.setRGB (i, j, Color.WHITE.getRGB ());
                } else{
                    // 绘制为黑色
                    buffimg.setRGB (i, j, Color.BLACK.getRGB ());
                }
            }
        }
        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_05(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        Graphics bg = buffimg.getGraphics();
        for(int i = (10+p/10); i < imgArray.length; i += (3+p/10)) {
            for(int j = (5+p/10);j < imgArray[i].length; j += (3+p/10)) {
                int num = imgArray[i][j];
                Color color = new Color (num);
                bg.setColor(color);
                int r1 = (int)(Math.random()*(40+p/5));
                int r2 = (int)(Math.random()*(30+p/5));
                bg.fillOval(i-(20+p/10), j-(15+p/10), Math.max(r1,r2), Math.min(r1,r2));
            }

        }
        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_06(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        for(int i = 0; i < imgArray.length; i++){
            for(int j = 0; j < imgArray[i].length; j++){
                int num = imgArray[i][j];
                Color color = new Color (num);
                int red = color.getRed ();
                int green = color.getGreen ();
                int blue = color.getBlue ();

                int rednext = red + (3+p/3);
                if(rednext > 255){
                    rednext = 255;
                }
                int greennext = green + (3+p/3);
                if(greennext > 255){
                    greennext = 255;
                }
                int bluenext = blue + (3+p/3);
                if(bluenext > 255){
                    bluenext = 255;
                }
                Color newColor = new Color (rednext, greennext, bluenext);
                // 缓冲绘制
                buffimg.setRGB (i, j, newColor.getRGB ());
            }
        }
        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_07(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        if(p<=50) {
            int[][] fls= {
                    {1,0,0,0,0},
                    {0,1,0,0,0},
                    {0,0,1,0,0},
                    {0,0,0,1,0},
                    {0,0,0,0,1},
            };

            for(int i=0;i<imgArray.length-4;i++) {
                for(int j=0;j<imgArray[i].length-4;j++) {
                    int redvalue = 0;
                    int greenvalue = 0;
                    int bluevalue = 0;
                    for(int k = 0; k < fls.length; k++) {
                        for (int l = 0; l < fls[k].length; l++) {
                            int rgbvalue = imgArray[i + k][j + l];
                            int red = (rgbvalue >> 16) & 0xFF;
                            int green = (rgbvalue >> 8) & 0xFF;
                            int blue = (rgbvalue) & 0xFF;
                            redvalue += red * fls[k][l];
                            greenvalue += green * fls[k][l];
                            bluevalue += blue * fls[k][l];

                        }
                    }
                    redvalue = redvalue/5;
                    greenvalue = greenvalue/5;
                    bluevalue = bluevalue/5;

                    if(redvalue>255)redvalue=255;
                    if(greenvalue>255)greenvalue=255;
                    if(bluevalue>255)bluevalue=255;
                    if(redvalue<0)redvalue=0;
                    if(greenvalue<0)greenvalue=0;
                    if(bluevalue<0)bluevalue=0;

                    Color newColor = new Color (redvalue, greenvalue, bluevalue);
                    buffimg.setRGB (i, j, newColor.getRGB ());
                }
            }
        }

        if(p>50) {
            float[][] fls= {
                    {1,0,0,0,0,0,0,0,0},
                    {0,1,0,0,0,0,0,0,0},
                    {0,0,1,0,0,0,0,0,0},
                    {0,0,0,1,0,0,0,0,0},
                    {0,0,0,0,1,0,0,0,0},
                    {0,0,0,0,0,1,0,0,0},
                    {0,0,0,0,0,0,1,0,0},
                    {0,0,0,0,0,0,0,1,0},
                    {0,0,0,0,0,0,0,0,1},
            };

            for(int i=0;i<imgArray.length-8;i++) {
                for(int j=0;j<imgArray[i].length-8;j++) {
                    int redvalue = 0;
                    int greenvalue = 0;
                    int bluevalue = 0;
                    for(int k=0;k<fls.length;k++) {
                        for (int l = 0; l < fls[k].length; l++) {
                            int rgbvalue = imgArray[i + k][j + l];
                            int red = (rgbvalue >> 16) & 0xFF;
                            int green = (rgbvalue >> 8) & 0xFF;
                            int blue = (rgbvalue) & 0xFF;
                            redvalue += red * fls[k][l];
                            greenvalue += green * fls[k][l];
                            bluevalue += blue * fls[k][l];
                        }
                    }
                    redvalue = redvalue/9;
                    greenvalue = greenvalue/9;
                    bluevalue = bluevalue/9;

                    if(redvalue>255)redvalue=255;
                    if(greenvalue>255)greenvalue=255;
                    if(bluevalue>255)bluevalue=255;
                    if(redvalue<0)redvalue=0;
                    if(greenvalue<0)greenvalue=0;
                    if(bluevalue<0)bluevalue=0;

                    Color newColor = new Color (redvalue, greenvalue, bluevalue);
                    buffimg.setRGB (i, j, newColor.getRGB ());
                }
            }
        }
        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_08(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        if(p<=50) {
            int[][] fls= {
                    {-1,-1,0},
                    {-1,0,1},
                    {0,1,1}
            };

            for(int i=0;i<imgArray.length-2;i++) {
                for(int j=0;j<imgArray[i].length-2;j++) {
                    int redvalue = 0;
                    int greenvalue = 0;
                    int bluevalue = 0;
                    for(int k = 0; k < fls.length; k++) {
                        for (int l = 0; l < fls[k].length; l++) {
                            int rgbvalue = imgArray[i + k][j + l];
                            int red = (rgbvalue >> 16) & 0xFF;
                            int green = (rgbvalue >> 8) & 0xFF;
                            int blue = (rgbvalue) & 0xFF;
                            redvalue += red * fls[k][l];
                            greenvalue += green * fls[k][l];
                            bluevalue += blue * fls[k][l];
                        }
                    }

                    if(redvalue>255)redvalue=255;
                    if(greenvalue>255)greenvalue=255;
                    if(bluevalue>255)bluevalue=255;
                    if(redvalue<0)redvalue=0;
                    if(greenvalue<0)greenvalue=0;
                    if(bluevalue<0)bluevalue=0;

                    Color newColor = new Color (redvalue, greenvalue, bluevalue);
                    buffimg.setRGB (i, j, newColor.getRGB ());
                }
            }
        }

        if(p>50) {
            float[][] fls= {
                    {-1,-1,-1,-1,0},
                    {-1,-1,-1,0,1},
                    {-1,-1,0,1,1},
                    {-1,0,1,1,1},
                    {0,1,1,1,1},
            };

            for(int i=0;i<imgArray.length-4;i++) {
                for(int j=0;j<imgArray[i].length-4;j++) {
                    int redvalue = 0;
                    int greenvalue = 0;
                    int bluevalue = 0;
                    for(int k=0;k<fls.length;k++) {
                        for (int l = 0; l < fls[k].length; l++) {
                            int rgbvalue = imgArray[i + k][j + l];
                            int red = (rgbvalue >> 16) & 0xFF;
                            int green = (rgbvalue >> 8) & 0xFF;
                            int blue = (rgbvalue) & 0xFF;
                            redvalue += red * fls[k][l];
                            greenvalue += green * fls[k][l];
                            bluevalue += blue * fls[k][l];
                        }
                    }

                    if(redvalue>255)redvalue=255;
                    if(greenvalue>255)greenvalue=255;
                    if(bluevalue>255)bluevalue=255;
                    if(redvalue<0)redvalue=0;
                    if(greenvalue<0)greenvalue=0;
                    if(bluevalue<0)bluevalue=0;

                    Color newColor = new Color (redvalue, greenvalue, bluevalue);
                    buffimg.setRGB (i, j, newColor.getRGB ());
                }
            }
        }
        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_09(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        if(p<=50) {
            int[][] fls= {
                    {-1,-1,-1},
                    {-1,9,-1},
                    {-1,-1,-1}
            };

            for(int i=0;i<imgArray.length-2;i++) {
                for(int j=0;j<imgArray[i].length-2;j++) {
                    int redvalue = 0;
                    int greenvalue = 0;
                    int bluevalue = 0;
                    for(int k = 0; k < fls.length; k++) {
                        for (int l = 0; l < fls[k].length; l++) {
                            int rgbvalue = imgArray[i + k][j + l];
                            int red = (rgbvalue >> 16) & 0xFF;
                            int green = (rgbvalue >> 8) & 0xFF;
                            int blue = (rgbvalue) & 0xFF;
                            redvalue += red * fls[k][l];
                            greenvalue += green * fls[k][l];
                            bluevalue += blue * fls[k][l];
                        }
                    }

                    if(redvalue>255)redvalue=255;
                    if(greenvalue>255)greenvalue=255;
                    if(bluevalue>255)bluevalue=255;
                    if(redvalue<0)redvalue=0;
                    if(greenvalue<0)greenvalue=0;
                    if(bluevalue<0)bluevalue=0;

                    Color newColor = new Color (redvalue, greenvalue, bluevalue);
                    buffimg.setRGB (i, j, newColor.getRGB ());
                }
            }
        }

        if(p>50) {
            float[][] fls= {
                    {-1,-1,-1,-1,-1},
                    {-1,-1,-1,-1,-1},
                    {-1,-1,25,-1,-1},
                    {-1,-1,-1,-1,-1},
                    {-1,-1,-1,-1,-1},
            };

            for(int i=0;i<imgArray.length-4;i++) {
                for(int j=0;j<imgArray[i].length-4;j++) {
                    int redvalue = 0;
                    int greenvalue = 0;
                    int bluevalue = 0;
                    for(int k=0;k<fls.length;k++) {
                        for (int l = 0; l < fls[k].length; l++) {
                            int rgbvalue = imgArray[i + k][j + l];
                            int red = (rgbvalue >> 16) & 0xFF;
                            int green = (rgbvalue >> 8) & 0xFF;
                            int blue = (rgbvalue) & 0xFF;
                            redvalue += red * fls[k][l];
                            greenvalue += green * fls[k][l];
                            bluevalue += blue * fls[k][l];
                        }
                    }

                    if(redvalue>255)redvalue=255;
                    if(greenvalue>255)greenvalue=255;
                    if(bluevalue>255)bluevalue=255;
                    if(redvalue<0)redvalue=0;
                    if(greenvalue<0)greenvalue=0;
                    if(bluevalue<0)bluevalue=0;

                    Color newColor = new Color (redvalue, greenvalue, bluevalue);
                    buffimg.setRGB (i, j, newColor.getRGB ());
                }
            }
        }
        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_10(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        for(int i=0;i<imgArray.length-1;i++) {
            for(int j=0;j<imgArray[i].length-1;j++) {
                int redvalue = 0;
                int greenvalue = 0;
                int bluevalue = 0;
                int k=i+1;
                int l=j+1;
                int rgbvalue1 = imgArray[i][j];
                int rgbvalue2 = imgArray[k][l];

                int red1 = (rgbvalue1>>16)&0xFF;
                int green1 = (rgbvalue1>>8)&0xFF;
                int blue1 = (rgbvalue1)&0xFF;

                int red2 = (rgbvalue2>>16)&0xFF;
                int green2 = (rgbvalue2>>8)&0xFF;
                int blue2 = (rgbvalue2)&0xFF;

                redvalue = red1-red2+128;
                greenvalue = green1-green2+128;
                bluevalue = blue1-blue2+128;

                int gray = (redvalue + greenvalue + bluevalue) / 3;

                if(gray>255)gray=255;
                if(gray<0)gray=0;

                Color newvalue = new Color(gray,gray,gray);
                buffimg.setRGB (k, l, newvalue.getRGB());

            }
        }

        for(int i=0;i<imgArray.length;i++) {
            int rgbvalue = imgArray[i][0];
            int gray = getRgbGray(rgbvalue);
            Color color = new Color(gray,gray,gray);
            buffimg.setRGB (i, 0, color.getRGB());
        }

        for(int j=0;j<imgArray[0].length;j++) {
            int rgbvalue = imgArray[0][j];
            int gray = getRgbGray(rgbvalue);
            Color color = new Color(gray,gray,gray);
            buffimg.setRGB (0, j, color.getRGB());
        }

        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_11(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        int[][] newArr = new int[imgArray[0].length][imgArray.length];
        for(int i = 0; i < imgArray.length; i++) {
            for (int j = 0; j < imgArray[i].length; j++) {
                newArr[j][imgArray.length-i-1] = imgArray[i][j];
            }
        }

        for(int i = 0; i < newArr.length; i++) {
            for (int j = 0; j < newArr[i].length; j++) {
                imgArray[i][j] = newArr[i][j];
                buffimg.setRGB(i, j, newArr[i][j]);
            }
        }
        return buffimg;
    }

    //根据二维数组 绘制图片
    public BufferedImage drawImage_12(int[][] imgArray){
        BufferedImage buffimg = new BufferedImage (imgArray.length, imgArray[0].length, BufferedImage.TYPE_INT_ARGB);
        int[][] newArr = new int[imgArray[0].length][imgArray.length];
        for(int i = 0; i < imgArray.length; i++) {
            for (int j = 0; j < imgArray[i].length; j++) {
                newArr[imgArray.length-j-1][i] = imgArray[i][j];
            }
        }

        for(int i = 0; i < newArr.length; i++) {
            for (int j = 0; j < newArr[i].length; j++) {
                imgArray[i][j] = newArr[i][j];
                buffimg.setRGB(i, j, newArr[i][j]);
            }
        }
        return buffimg;
    }

    //根据二维数组 绘制图片
    public void drawImage_13(int[][] imgArray){
        imgPanel.width += p/5;
        imgPanel.height += p/5;
    }

    //根据二维数组 绘制图片
    public void drawImage_14(int[][] imgArray){
        imgPanel.width -= p/5;
        imgPanel.height -= p/5;
    }

}